﻿//--------------------------------------------------------------------------------------------------------------------------------------------------------
// <copyright file="SecurityController.cs">
//     Copyright © 2010 Oli Francis
//     This source code is subject to the terms and conditions of the MIT license. A copy of the license can be found in the license.txt 
//     file at the root of this distribution. By using this source code in any fashion, you are agreeing to be bound by the terms of the MIT License. 
//     You must not remove this notice from the software.
// </copyright>
// <author>Oli</author>
//-------------------------------------------------------------------------------------------------------------------------------------------------------

namespace TeamBrain.Web.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    using TeamBrain.Domain.Services.Exceptions;
    using TeamBrain.Domain.Services.Security;

    /// <summary>
    /// Controller class to handle security related requests
    /// </summary>
    public class SecurityController : Controller
    {
        private readonly ISecurityService securityService;

        /// <summary>
        /// Initializes a new instance of the <see cref="SecurityController"/> class.
        /// </summary>
        public SecurityController(ISecurityService securityService)
        {
            this.securityService = securityService;
        }

        /// <summary>
        /// Handles a get request for the sign-in form
        /// </summary>
        [HttpGet]
        public ActionResult SignIn()
        {
            return View("SignIn");
        }

        /// <summary>
        /// Handles a form post from the sign-in form
        /// </summary>
        [HttpPost]
        public ActionResult SignIn(string email, string password, bool rememberMe)
        {
            if (String.IsNullOrEmpty(email))
            {
                ModelState.AddModelError("email", "Please enter your email address");
            }

            if (String.IsNullOrEmpty(password))
            {
                ModelState.AddModelError("password", "Please enter your password");
            }

            if (ModelState.IsValid)
            {
                var result = this.securityService.AttemptSignIn(email, password, rememberMe);

                switch (result)
                {
                    case SignInOutcome.Success:
                        return RedirectToRoute(new { Controller = "Profile", Action = "Index" });

                    case SignInOutcome.CredentialsIncorrect:
                        ModelState.AddModelError("signin", "Sorry, we weren't able to sign you in with those details");
                        break;

                    case SignInOutcome.AccountLocked:
                        ModelState.AddModelError("signin", "Sorry, your account has been locked. You'll need to wait 10 minutes and then try again.");
                        break;

                    case SignInOutcome.AccountDisabled:
                        ModelState.AddModelError("signin", "Your account has been disabled");
                        break;
                }
            }

            return View("SignIn");
        }

        /// <summary>
        /// Handles a get request for the forgotten password form
        /// </summary>
        [HttpGet]
        public ActionResult ForgottenPassword()
        {
            return View("ForgottenPassword");
        }

        [HttpPost]
        public ActionResult ForgottenPassword(string email)
        {
            if (String.IsNullOrEmpty(email))
            {
                this.ModelState.AddModelError("email", "Please enter your email address");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    this.securityService.RequestPasswordReset(email);
                    return RedirectToAction("ForgottenPasswordEmailSent");
                }
                catch (EmailAddressNotRecognisedException)
                {
                    this.ModelState.AddModelError("email", "Sorry, that email address was not recognised");
                }
            }

            return View("ForgottenPassword");
        }

        /// <summary>
        /// Forgottens the password email sent confirmation page.
        /// </summary>
        [HttpGet]
        public ActionResult ForgottenPasswordEmailSent()
        {
            return View("ForgottenPasswordEmailSent");
        }

        [HttpGet]
        public ActionResult ResetPassword(string token)
        {
            var status = this.securityService.CheckPasswordResetToken(token);

            switch(status)
            {
                case TokenStatus.Expired:
                    return RedirectToAction("PasswordResetExpired");

                case TokenStatus.NotRecognised:
                    return RedirectToAction("PasswordResetInvalid");
                
                default:
                    return this.View("ResetPassword");
            }
        }

        [HttpGet]
        public ActionResult PasswordResetExpired()
        {
            throw new NotImplementedException();
        }
    }
}
